﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Data;
using System.ComponentModel;

namespace DKSEO
{
    public static class Function
    {
        public static Data weblinks(string stream)
        {
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Data));
                //FileStream loadStream = new FileStream("sample.xml", FileMode.Open, FileAccess.Read);
                TextReader text = new StringReader(stream);
                Data loadedObject = (Data)serializer.Deserialize(text);
                return loadedObject;
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }
        public static DataTable ToDataTable<T>(IList<T> data)
        {
            PropertyDescriptorCollection properties =
                TypeDescriptor.GetProperties(typeof(T));
            DataTable table = new DataTable();
            foreach (PropertyDescriptor prop in properties)
                table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
            foreach (T item in data)
            {
                DataRow row = table.NewRow();
                foreach (PropertyDescriptor prop in properties)
                    row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
                table.Rows.Add(row);
            }
            return table;
        }
    }
    [XmlRoot("data"), Serializable]
    public class Data
    {
        [XmlElement("links")]
        public Links links { get; set; }
    }

    public class Links
    {
        [XmlAttribute("random")]
        public string random { get; set; }
        [XmlElement("link")]
        public List<Link> links { get; set; }
    }
    public class Link
    {
        [XmlAttribute("chitieuId")]
        public string chitieuId { get; set; }
        [XmlAttribute("pageId")]
        public string pageId { get; set; }
        [XmlAttribute("pageURL")]
        public string pageURL { get; set; }
    }
}
